<template>
	
	<view class="wrap">
			<view class="dev-area">
			
			   <view class="dev-cart">
					<view class="">
						<view class="dev-name">温度</view>
						<image class="dev-logo"src="../../static/temp.png"></image>
					</view>
					<view class="dev-data">{{temp}}.C</view>
			   </view>
			   
			   
			   <view class="dev-cart">
			   			<view class="">
			   				<view class="dev-name">湿度</view>
			   				<image class="dev-logo"src="../../static/humi.png"></image>
			   			</view>
			   			<view class="dev-data">{{humi}}%</view>
			   </view>
			   <view class="dev-cart">
			   			<view class="">
			   				<view class="dev-name">LED</view>
			   				<image class="dev-logo"src="../../static/LED.png"></image>
			   			</view>
			   			<switch :checked="led" @change="onLedSwitch"></switch>
			   </view>
			   
				
				
			</view>
	</view>
	
</template>

<script>
	const {
		createCommonToken
	} = require('@/key.js')
	export default {
		data() {
			return {
				temp:'',
				humi:'',
				led:"",
				title: 'Hello',
				token:""
			}
		},
		onLoad() {
			const params = {
				author_key: "OzWcPbNJab0HGeipMVU/L7Znxfjt2t6fAXlZsB/3b/LhrHzUVGI1d+78XHf12cgmyYEuY1k+2r69v5XJ7lJY4A==",
				version: "2022-05-01",
				user_id: "261462",
			}
			this.token = createCommonToken(params);

		},
		onShow(){
			this.fetchData();
		},
		methods: {
			fetchData(){
				uni.request({
					  url: 'https://iot-api.heclouds.com/thingmodel/query-device-property',
					  method:"GET",
					  data: {
						  product_id:"Y4l9I4hhmh",
						  device_name:'d1',
					  },
					  header: {
					    'authorization': this.token // 默认值
					  },
					 //  success (res) {  这样写不行，明显的错误
					 //    console.log(res.data);
						// this.temp =res.data.data[2].value;
					 //    console.log(res.data.data[2].value);
						// this.humi = res.data.data[0].value;
						// console.log(res.data.data[0].value);
						// this.led =res.data.data[1].value === true;
						// console.log(this.led);
					 //  }
					  success :(res) =>{
							// console.log(res.data);
							this.temp =res.data.data[2].value;
							// console.log(res.data.data[2].value);
							this.humi = res.data.data[0].value;
							// console.log(res.data.data[0].value);
							this.led =res.data.data[1].value === true;
							// console.log(this.led);
					  }
					  
				})
				
				
			},
			onLedSwitch(event){
				// let value = console.log(event); //获取值的状态
				
				 console.log(event.detail.value); //获取值的状态
				// let value = console.log(event.detail.value); //获取值的状态  //！！！逆天的代码  2024年3月7日
				let value = event.detail.value;
				uni.request({
					  url: 'https://iot-api.heclouds.com/thingmodel/set-device-desired-property', 
					  method:"POST",
					  data: {
						  product_id:"Y4l9I4hhmh",
						  device_name:'d1',
						  params: { "led":value }
					  },
					 header: {
					  'authorization': this.token // 默认值
					},
					  success :() =>{
						 console.log("LED-"+ ( value ==  true ? "ON":"OFF"));
					  }
					  
				})
				
				
			}

		}
	}
</script>

<style>
	/* 设置容器的内边距为30rpx */
	.wrap {
	    padding: 30rpx;
	}
	
	/* 设置开发者区域的布局为弹性盒子，子元素之间平均分布，自动换行 */
	.dev-area {
	    display: flex;
	    justify-content: space-between;
	    flex-wrap: wrap;
	}
	
	/* 设置内容区域的布局为弹性盒子，子元素垂直居中对齐，水平居中对齐 */
	.content {
	    display: flex;
	    flex-direction: column;
	    align-items: center;
	    justify-content: center;
	}
	
	/* 设置开发者卡片的高度为150rpx，宽度为320rpx，圆角为30rpx，上外边距为30rpx，布局为弹性盒子，子元素水平平均分布，垂直居中对齐，阴影效果 */
	.dev-cart {
	    height: 150rpx;
	    width: 320rpx;
	    border-radius: 30rpx;
	    margin-top: 30rpx;
	    display: flex;
	    justify-content: space-around;
	    align-items: center;
	    box-shadow: 0 0 15rpx #ccc;
	}
	
	/* 设置开发者名称的字体大小为20rpx，颜色为#6d6d6d，文本居中对齐 */
	.dev-name {
	    font-size: 20rpx;
	    color: #6d6d6d;
	    text-align: center;
	}
	
	/* 设置开发者logo的宽度为70rpx，高度为70rpx */
	.dev-logo {
	    width: 70rpx;
	    height: 70rpx;
	}
	
	/* 设置开发者数据的字体大小为50rpx，颜色为#8f8f94 */
	.dev-data {
	    font-size: 50rpx;
	    color: #8f8f94;
	}

</style>